package com.nondo.dean.dealer.controller.api.admin;

import com.nondo.dean.common.annotation.ApiVersion;
import com.nondo.dean.common.tool.ValidateUtil;
import com.nondo.dean.common.web.Page;
import com.nondo.dean.common.web.Query;
import com.nondo.dean.common.web.View;
import com.nondo.dean.dealer.domain.Activity;
import com.nondo.dean.dealer.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author xiaofan
 * @Description: 活动管理
 * @throws
 * @date 2018-05-1111:15
 * @Version 0.1
 */
@RestController("apiAdminDealerActivity")
@RequestMapping("/{v}/admin/activity")
public class ActicityController {

    @Autowired
    ActivityService activityService;


    /**
     * 列表
     *
     * @param request
     * @param response
     * @param query
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/list")
    public View<Page<Activity>> list(HttpServletRequest request, HttpServletResponse response, @RequestBody Query<Activity> query) {
        View<Page<Activity>> view = new View<Page<Activity>>(20000, "获取数据成功");

        Activity activity = query.getData();
        if(activity == null){
            activity = new Activity();
        }
        Page<Activity> page = new Page<Activity>(query.getPage(), query.getLimit(), activityService.count(activity));

        List<Activity> gradeList = activityService.list(query.getOffset(), query.getLimit(), activity);

        page.setContent(gradeList);

        view.setData(page);

        return view;
    }

    /**
     * 添加活动
     * @param request
     * @param response
     * @param activity 产品类型信息 {@link Activity}
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/add")
    public View<Activity> add(HttpServletRequest request, HttpServletResponse response,
                                 @Validated @RequestBody Activity activity, BindingResult result) {
        View<Activity> view = new View<Activity>(20000, "添加成功");

        if (result.hasErrors()) {
            view.setCode(40000);
            view.setMessage(ValidateUtil.first(result.getAllErrors()));
        } else {
            Activity activityDb = this.activityService.selectName(activity.getName());
            if (activityDb != null) {
                return View.of500Error("活动名称已存在");
            }
            this.activityService.add(activity);
        }
        return view;
    }

    /**
     * 查看活动详情
     * @param request
     * @param response
     * @param id
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/info/{id}")
    public View<Activity> info(HttpServletRequest request, HttpServletResponse response, @PathVariable("id") Integer id) {
        View<Activity> view = new View<Activity>(20000, "获取数据成功");

        Activity activity = null;
        if (id != null) {
            activity = activityService.select(id);
        } else {
            activity = new Activity();
        }

        view.setData(activity);
        return view;
    }


    /**
     * 删除活动
     * @param request
     * @param response
     * @param id
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/delete/{id}")
    public View<Integer> delete(HttpServletRequest request, HttpServletResponse response, @PathVariable("id") Integer id){
        View view =new View(2000,"删除成功");

        Activity activity=activityService.select(id);
        activity.setDelFlag(Activity.DEL_FLAG_DELETE);
        this.activityService.update(activity);


        return view;
    }



}
